package com.imddy.spbatch001.utils.sql;

import java.util.ArrayList;
import java.util.List;

//===== 条件组类 =====
public class ConditionGroup {
    private final List<Object> conditions = new ArrayList<>();
    private final String combinator; // AND / OR
    private boolean parenWrapped = true;

    public ConditionGroup(String combinator) {
        this.combinator = combinator;
    }

    public static ConditionGroup and() {
        return new ConditionGroup("AND");
    }

    public static ConditionGroup or() {
        return new ConditionGroup("OR");
    }

    public ConditionGroup add(Condition condition) {
        conditions.add(condition);
        return this;
    }

    public ConditionGroup add(ConditionGroup group) {
        conditions.add(group);
        return this;
    }

    public List<Object> getConditions() {
        return conditions;
    }

    public ConditionGroup wrapWithParentheses(boolean wrap) {
        this.parenWrapped = wrap;
        return this;
    }

    public String getCombinator() {
        return combinator;
    }

    public boolean isParenWrapped() {
        return parenWrapped;
    }

    @Override
    public String toString() {
        if (conditions.isEmpty()) return "";
        String joined = conditions.stream()
                .map(Object::toString)
                .reduce((a, b) -> a + " " + combinator + " " + b)
                .orElse("");
        return (parenWrapped ? "(" : "") + joined + (parenWrapped ? ")" : "");
    }
}
